package com.freecloud.framework.request;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.freecloud.framework.exceptions.RRException;
import com.freecloud.framework.xss.SQLFilter;
import lombok.Data;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 查询参数
 *
 * @author maomao
 * @email 138383877@qq.com
 */
@Data
public class Query extends LinkedHashMap<String, Object> {
	private static final long serialVersionUID = 1L;
    /**
     * 当前页码
     */
    private int page;
    /**
     * 每页条数
     */
    private int limit;

    public Query(Map<String, Object> params){
        this.putAll(params);

        //分页参数
        this.page = Integer.parseInt(params.get("page").toString());
        this.limit = Integer.parseInt(params.get("limit").toString());
        this.put("offset", (page - 1) * limit);
        this.put("page", page);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        if(params.get("sidx") != null )
        {
            String sidx = params.get("sidx").toString();
            this.put("sidx", SQLFilter.sqlInject(sidx));
        }

        if(params.get("order") != null)
        {
            String order = params.get("order").toString();
            this.put("order", SQLFilter.sqlInject(order));
        }
    }




    /**
     * 获取Spring Data JPA Pageable对象，用于分页查询
     * @param params
     * @param defaultSidx
     * @return
     */
    public static Pageable getPageable(Map<String, Object> params,String defaultSidx)
    {
        int page = 1,limit = 10,offset = 0;

        Sort sort;
        Sort.Direction direction = Sort.Direction.DESC;

        if(ObjectUtil.isNotNull(params.get("page"))){
            page = Integer.parseInt(params.get("page").toString());
        }
        if(ObjectUtil.isNotNull(params.get("limit"))){
            limit = Integer.parseInt(params.get("limit").toString());
        }
        if(page < 0 || limit < 0){
            throw new RRException("page limit 参数错误！");
        }
        if(StrUtil.isEmpty(defaultSidx)){
            defaultSidx = "id";
        }
        String sidx = defaultSidx;

        offset = (page - 1) * limit;

        if(params.get("order") != null)
        {
            String order = SQLFilter.sqlInject(params.get("order").toString());
            direction = Sort.Direction.fromString(order);
        }

        if(ObjectUtil.isNotNull(params.get("sidx")) && StrUtil.isNotEmpty(params.get("sidx").toString()))
        {
            sidx = SQLFilter.sqlInject(params.get("sidx").toString());
        }

        sort = Sort.by(direction,sidx);

        return PageRequest.of(offset,limit,sort);
    }

    public static Example getExample(Map<String, Object> params,Class classs){
        //处理日期条件查询
        return Example.of(JSONUtil.parseObj(params).toBean(classs));
    }
}
